package com.example.badminton.controller;

import com.example.badminton.common.Result;
import com.example.badminton.entity.Order;
import com.example.badminton.service.OrderService;
import com.example.badminton.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/order")
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/create")
    public Result<Order> createOrder(@RequestBody Order order) {
        Long userId = UserUtils.getCurrentUserId();
        
        order.setUserId(userId);
        orderService.save(order);
        return Result.success(order);
    }

    @GetMapping("/list")
    public Result<List<Order>> getOrderList() {
        Long userId = UserUtils.getCurrentUserId();
        
        List<Order> orders = orderService.getOrdersByUserId(userId);
        return Result.success(orders);
    }

    @GetMapping("/{id}")
    public Result<Order> getOrderById(@PathVariable Long id) {
        Long userId = UserUtils.getCurrentUserId();
        
        Order order = orderService.getById(id);
        if (order == null) {
            return Result.error("订单不存在");
        }
        
        // 检查权限：只能查看自己的订单
        if (!order.getUserId().equals(userId) && !UserUtils.isAdmin()) {
            return Result.error("无权限查看此订单");
        }
        
        return Result.success(order);
    }

    @PutMapping("/{id}")
    public Result<Order> updateOrder(@PathVariable Long id, @RequestBody Order order) {
        Long userId = UserUtils.getCurrentUserId();
        
        Order existingOrder = orderService.getById(id);
        if (existingOrder == null) {
            return Result.error("订单不存在");
        }
        
        // 检查权限：只能修改自己的订单
        if (!existingOrder.getUserId().equals(userId) && !UserUtils.isAdmin()) {
            return Result.error("无权限修改此订单");
        }
        
        order.setId(id);
        order.setUserId(userId);
        orderService.updateById(order);
        return Result.success(order);
    }

    @DeleteMapping("/{id}")
    public Result<String> deleteOrder(@PathVariable Long id) {
        Long userId = UserUtils.getCurrentUserId();
        
        Order order = orderService.getById(id);
        if (order == null) {
            return Result.error("订单不存在");
        }
        
        // 检查权限：只能删除自己的订单
        if (!order.getUserId().equals(userId) && !UserUtils.isAdmin()) {
            return Result.error("无权限删除此订单");
        }
        
        orderService.removeById(id);
        return Result.success("删除成功");
    }

    @PostMapping("/{id}/pay")
    public Result<String> payOrder(@PathVariable Long id) {
        Long userId = UserUtils.getCurrentUserId();
        
        Order order = orderService.getById(id);
        if (order == null) {
            return Result.error("订单不存在");
        }
        
        // 检查权限：只能支付自己的订单
        if (!order.getUserId().equals(userId)) {
            return Result.error("无权限操作此订单");
        }
        
        // 这里可以添加支付逻辑
        order.setStatus(1); // 已支付
        orderService.updateById(order);
        return Result.success("支付成功");
    }

    @PostMapping("/{id}/cancel")
    public Result<String> cancelOrder(@PathVariable Long id) {
        Long userId = UserUtils.getCurrentUserId();
        
        Order order = orderService.getById(id);
        if (order == null) {
            return Result.error("订单不存在");
        }
        
        // 检查权限：只能取消自己的订单
        if (!order.getUserId().equals(userId) && !UserUtils.isAdmin()) {
            return Result.error("无权限操作此订单");
        }
        
        order.setStatus(2); // 已取消
        orderService.updateById(order);
        return Result.success("订单已取消");
    }
} 